#include <syncupconfig.h>
#include <async_queue.h>
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#ifdef OS_WIN
#include <windows.h>
#else
#include <sys/select.h>
#endif

static inline void thread_msleep(int msecs)
{
#ifdef OS_WIN
    Sleep(msecs);
#else
    struct timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = msecs * 1000;
    select(0, NULL, NULL, NULL, &tv);
#endif
}

static struct async_queue queue = ASYNC_QUEUE_INITIALIZER(queue);

static void *
thread_func(void *noarg)
{
    struct timespec ts = {5, 0};
    int data;

    while (1) {
        data = GPOINTER_TO_INT(async_queue_timeout_pop(&queue, &ts));
        if (data == 0) {
            break;
        }
        printf("data = %d\n", data);
    }
    return NULL;
}

int main()
{
    pthread_t tid;

    async_queue_push(&queue, GINT_TO_POINTER(1));

    if (pthread_create(&tid, NULL, thread_func, NULL)) {
        fprintf(stderr, "can't create thread\n");
        exit(1);
    }

    thread_msleep(2000);

    async_queue_push(&queue, GINT_TO_POINTER(2));
    async_queue_push(&queue, GINT_TO_POINTER(3));


    pthread_join(tid, NULL);
    return 0;
}
